/*
 * This servlet controls the activation from the activation link.
 * 
 */
package controllers.classes;

import classes.HibernateUtil;
import dao.ActivationDao;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Activation;

/**
 *
 * @author Achie
 * 
 */
public class ActivationController extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String activationKey = request.getParameter("key"); //Get the activation key from the url

        HibernateUtil hu = new HibernateUtil();
        ActivationDao ad = new ActivationDao(hu.openSession());
        List<Activation> activationlist = ad.findByKey(activationKey); //Get the activation object with the activationKey

        if (activationlist.size() == 1) { //if it is unique, activate
            if (activationlist.get(0).isActivated()) { //if already activated do nothing and he will be redirect!
                //nothing (redirect beneath)
            }
            Activation act = activationlist.get(0);
            act.setActivated(true);
            ad.update(act);
        }
        hu.close();
        response.sendRedirect("Index");

    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
